package com.xrit.factory;

import com.xrit.inter.Car;
import java.io.InputStream;
import java.util.Properties;

public class CarFactory {
    /**
     * 简单的工厂模式
     */
    /*public static Car createCar(String carName) throws Exception {
        if ("falali".equals(carName)){
            return new Falali();
        }else if("benchi".equals(carName)){
            return new Benchi();
        }
        return null;
    }*/

    /**
     * 工厂+反射，实现解耦
     */
    public static Car createCar(String firlLocation) throws Exception {
        //获取类的加载器(目的是我们要使用它来加载配置文件)
        ClassLoader classLoader = CarFactory.class.getClassLoader();
        //读取配置文件
        InputStream is = classLoader.getResourceAsStream(firlLocation);
        //借助Properties对象加载流的数据
        Properties properties = new Properties();
        properties.load(is);
        //取数据
        String className = properties.getProperty("className");

        //反射获取对象
        Class<?> c = Class.forName(className);
        //直接简版创建对象
        Car car = (Car) c.newInstance();
        return car;
    }
}
